namespace TSL.Utility.Json
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;

    public class JavaScriptObject : IJavaScriptObject
    {
        private Dictionary<string, IJavaScriptObject> list = new Dictionary<string, IJavaScriptObject>();

        public void Add(string key, IJavaScriptObject value)
        {
            this.list.Add(key, value);
        }

        public bool Contains(string key)
        {
            return this.list.ContainsKey(key);
        }

        public override string ToString()
        {
            return this.Value;
        }

        public bool IsFixedSize
        {
            get
            {
                return false;
            }
        }

        public IJavaScriptObject this[string key]
        {
            get
            {
                return this.list[key];
            }
            set
            {
                if (!this.Contains(key))
                {
                    throw new ArgumentException("The specified key does not exists in this collection.", "key");
                }
                this.list[key] = value;
            }
        }

        public string[] Keys
        {
            get
            {
                string[] array = new string[this.list.Count];
                this.list.Keys.CopyTo(array, 0);
                return array;
            }
        }

        public string Value
        {
            get
            {
                return JavaScriptSerializer.Serialize(this.list);
            }
        }
    }
}

